using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class LargerWidgets : System.Web.UI.Page
{
    HtmlForm frmObj;

    protected void Page_PreInit(object sender, EventArgs e)
    {
        if (Session["Theme"] == null)
        {
            Session["Theme"] = GlobalValues.DefaultTheme;
            this.Theme = "BlueTheme";
        }
        else
            this.Theme = Session["theme"].ToString();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        HDDatabase hdd = new HDDatabase();
        security hdds = new security();
        Dashboards DB = new Dashboards();
        DataTable dt = new DataTable();
        try
        {
            setPageTitle();
            if (Session["dtWidgetData"] == null)
                Session.Add("dtWidgetData", null);

            if (Session["userid"] == null) Response.Redirect(hdds.SessionExpiryURL);
            if (Session["userid"].ToString() == "") Response.Redirect(hdds.SessionExpiryURL);

            if (this.Form.FindControl("form1") != null)
            {
                frmObj = (HtmlForm)this.Form.FindControl("form1");
            }
            dt = hdd.GetDataTable("select * from sy_widgets where af_row_id = '" + Request.QueryString["PK"].ToString() + "'");
            string sTitle = dt.Rows[0]["header"].ToString();
            if (Session["pageTitle"] != null)
                this.Title = Session["pageTitle"].ToString() + " - " + sTitle;
            else
                this.Title = sTitle;

            ArrayList list = new ArrayList();
            list.Add(Request.QueryString["PK"].ToString());
            
            DB.GenerateDashboards(ref td_dashboard, list, 1, 1, frmObj,null);

            string Pg_Icon = string.Empty, Val = null;
            try
            {
                if (dt.Rows[0]["source_type"].ToString().ToLower() == "report")
                {
                    Val = hdd.GetColumnValue("select page_icon from sy_pg where object_id = (select top(1)object_id from sy_report_att where rpt_id = '" + dt.Rows[0]["source"].ToString() + "');");
                }
                else if (dt.Rows[0]["source_type"].ToString().ToLower() == "query" && hdd.GetColumnValue("select primary_object_id from sy_report_sql where af_row_id = '" + dt.Rows[0]["source"].ToString() + "';") != null)
                {
                    Val = hdd.GetColumnValue("select page_icon from sy_pg where object_id = (select object_id from sy_obj where af_row_id = (select primary_object_id from sy_report_sql where af_row_id = '" + dt.Rows[0]["source"].ToString() + "'));");
                }
            }
            catch
            { }
            Pg_Icon = Val == null ? "Default.PNG" : Val == "" ? "Default.PNG" : Val;
            lblData_Header.Text = "&nbsp;<img src=\"Images/MBIcons/Medium/" + Pg_Icon + "\">&nbsp;" + HttpUtility.HtmlEncode(sTitle);
        }
        catch (Exception ex)
        {
            LogWriter.WriteLog("PageLoad WidgetsView: Error : " + ex.Message);
        }
        finally
        {
            hdd = null;
            hdds = null;
            DB = null;
            dt.Dispose();
        }
    }
    private void setPageTitle()
    {
        try
        {
            if (Session["pageTitle"] != null)
            {
                Page.Header.Title = Session["pageTitle"].ToString();
            }
            else
            {
                if (System.IO.File.Exists(HttpContext.Current.Server.MapPath(".") + "\\maxbloxtitle.ini"))
                {
                    System.IO.StreamReader sr = new System.IO.StreamReader(HttpContext.Current.Server.MapPath(".") + "\\maxbloxtitle.ini");
                    Session["pageTitle"] = sr.ReadLine();
                    Page.Header.Title = Session["pageTitle"].ToString();
                    sr.Close();
                    sr = null;
                }
            }
        }
        catch (Exception e)
        {
            LogWriter.WriteLog("Error in setPageTitle function. Error: " + e.Message);
        }
    }
}
